package com.intelligent.controller;

import com.intelligent.dto.UserPageQueryDTO;
import com.intelligent.entity.User;
import com.intelligent.feign.DeptApi;
import com.intelligent.service.UserService;
import com.intelligent.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    DeptApi deptApi;

    @GetMapping
    public Result<User> details(@RequestParam("id") int id) {
        User user = userService.getById(id);
        user.setRoleName(userService.getRoleName(user.getUserId()));
        user.setDeptName(deptApi.getById(user.getDeptId()).getDepartmentName());
        return Result.success(user);
    }

    @GetMapping("page")
    public Result<Map<String, Object>> page(
            @RequestParam(value = "pageNum", required = false) String pageNumStr,
            @RequestParam(value = "pageSize", required = false) String pageSizeStr,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "status", required = false) String statusStr,
            @RequestParam(value = "deptId", required = false) String deptIdStr,
            @RequestParam(value = "createTime1", required = false) List<String> createTimeBracket,
            @RequestParam(value = "createTime2", required = false) List<String> createTimePlain) {
        UserPageQueryDTO query = new UserPageQueryDTO();
        long pn = 1, ps = 5;
        try {
            if (pageNumStr != null && !pageNumStr.isEmpty())
                pn = Long.parseLong(pageNumStr);
        } catch (Exception ignored) {
        }
        try {
            if (pageSizeStr != null && !pageSizeStr.isEmpty())
                ps = Long.parseLong(pageSizeStr);
        } catch (Exception ignored) {
        }
        query.setPageNum(pn < 1 ? 1 : pn);
        query.setPageSize(ps < 1 ? 5 : ps);
        query.setKeyword(keyword);
        try {
            if (statusStr != null && !statusStr.isEmpty())
                query.setStatus(Integer.parseInt(statusStr));
        } catch (Exception ignored) {
        }
        try {
            if (deptIdStr != null && !deptIdStr.isEmpty())
                query.setDeptId(Integer.parseInt(deptIdStr));
        } catch (Exception ignored) {
        }

        java.util.List<String> merged = new java.util.ArrayList<>();
        if (createTimeBracket != null)
            merged.addAll(createTimeBracket);
        if (createTimePlain != null)
            merged.addAll(createTimePlain);
        if (!merged.isEmpty()) {
            if (merged.size() > 2)
                merged = merged.subList(0, 2);
            query.setCreateTime(merged);
        }

        Map<String, Object> data = userService.pageUsers(query);
        return Result.success(data);
    }

    @PostMapping("/add")
    public Result<Long> add(@RequestBody User user) {
        Long id = userService.addUser(user);
        return Result.success(id);
    }

    @PutMapping("/user")
    public Result<Void> update(@RequestBody User user) {
        userService.updateUser(user);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return Result.success();
    }

    @GetMapping("list")
    public Result<List<User>> list() {
        return Result.success(userService.list());
    }
}
